package cn.geoportal.benoserver.service.impl;

import cn.geoportal.benoserver.entity.vo.iot.IotInfoAdd;
import cn.geoportal.benoserver.entity.vo.iot.IotInfoGet;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.geoportal.benoserver.entity.IotInfo;
import cn.geoportal.benoserver.service.IotinfoService;
import cn.geoportal.benoserver.mapper.IotinfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 */
@Service
public class IotinfoServiceImpl extends ServiceImpl<IotinfoMapper, IotInfo> implements IotinfoService{

    @Autowired
    private IotinfoMapper iotinfoMapper;

    /**
     *
     * @param page
     * @param wrapper
     * @return
     */
    @Override
    public IPage<IotInfo> queryIotInfoPage(Page<IotInfo> page, QueryWrapper<IotInfo> wrapper) {
        return iotinfoMapper.queryIotInfoPage(page, wrapper);
    }

    /**
     *
     * @param iotinfo
     * @return
     */
    @Override
    public int add(IotInfoAdd iotinfo) {
        String geomStr = "POINT" + "(" + iotinfo.getLng() + " " + iotinfo.getLng() + ")";
        QueryWrapper<IotInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("name", iotinfo.getName());
        if(iotinfoMapper.selectCount(wrapper)>0){
            return 0;
        }
        return iotinfoMapper.addIot(iotinfo.getName(), iotinfo.getCode(), iotinfo.getAddress(), iotinfo.getDescription(), iotinfo.getTypeid(),
                iotinfo.getGltfUrl(), iotinfo.getVideoUrl(), iotinfo.getTemp(), iotinfo.getHumidity(), iotinfo.getHeight(), iotinfo.getLat(), iotinfo.getLng(), geomStr);
    }

    /**
     *
     * @param id
     * @return
     */
    @Override
    public IotInfoGet getIotInfoById(Integer id) {
        return iotinfoMapper.getIotInfoById(id);
    }

    /**
     *
     * @param iotinfo
     * @return
     */
    @Override
    public int updateIotInfo(IotInfo iotinfo) {
        QueryWrapper<IotInfo> wrapper = new QueryWrapper<>();
        wrapper.ne("id", iotinfo.getId()).eq("name", iotinfo.getName());
        if(iotinfoMapper.selectCount(wrapper)>0){
            return -1;
        }
        return iotinfoMapper.updateById(iotinfo);
    }

    /**
     *
     * @param id
     * @return
     */
    @Override
    public int deleteIotInfo(Integer id) {
        return iotinfoMapper.deleteById(id);
    }

    @Override
    public List<IotInfo> findWithDistance(double lat, double lng, double radius) {
        String pt = "POINT" + "(" + lng + " " + lat + ")";
        return iotinfoMapper.findDistanceWithIn(pt, radius);
    }
}




